package com.pro5.test.pro5demo.controller;

import com.pro5.test.pro5demo.domain.Pro5Goods;
import com.pro5.test.pro5demo.dto.PageResult;
import com.pro5.test.pro5demo.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by fangjicai on 2020/1/3.
 */

@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/items")
    @ResponseBody
    public PageResult<Pro5Goods> getGoods(@RequestParam(name = "keywords",required = false,defaultValue = "") String keywords,
                                          @RequestParam(name = "priceRange",defaultValue = "7200",required = false) BigDecimal priceRange,
                                          @RequestParam(name = "page",defaultValue = "1",required = false) Integer page){
        PageResult<Pro5Goods>  goods= searchService.search(keywords,priceRange,page);
        return goods;
    }

    @GetMapping("/toList")
    public String toList(){
        return "list";
    }

    @GetMapping("/toAgg")
    public String toAgg(){
        return "agg";
    }

    //品牌聚合，求每个品牌的商品数量和平均价格 brand goodNum avgPrice
    @GetMapping("aggBrand")
    @ResponseBody
    public List<Map<String,Object>> getAggBrand(){
       List<Map<String,Object>> maps= this.searchService.getAggBrand();
       return maps;
    }

}
